Code Tricks
Although RSL is a robust, fully functioning scripting language, that only Robot Battle robots understand, it has its limitations when compared to other computer languages. This section will be dedicated to those tricks of the trade that have been developed over the years by players attempting to get more out of RSL. Passing and Returning Values Passing and returning values is a very important part of building useful, reusable functions in any language. Passing a value, or "passing by value" is the method by which the value of a variable is passed to a function or subroutine. Returning a value is simply when the called function or routine "returns" a value through the calling function. Since RSL doesn't support either, we have to get creative and simulate the passing and returning of values to create reusable functions. Example1: Probably the best example for passing single values to a function in RSL is MinDegreesRight{} in the sample robot Zag.prg. This very useful function accepts the variables destAngle and startAngle, and returns the value rightDegrees, which tells the robot the shortest amount of rotation required to get to a certain angle. The value in rightDegrees can be applied to , , and commands. Also applies to , etc... MinDegreesRight { # Use the modulus operator (%) to ensure the result is from -360 to 360 difference = (destAngle - startAngle) % 360 # Figure out how much would be needed to rotate right rightDegrees = (difference + 360) % 360 # If this is more than 180, left rotation would be better # so set rightDegrees to a negative value. if( rightDegrees > 180 ) rightDegrees = rightDegrees - 360 endif } How would this function be called? It's this simple: Say you want to rotate your robot radar towards the of a robot that just pinged you. destangle = _pingheading //Pass value of _pingheading to MinDegreesRight startangle = _radaraim //Pass value of _radaraim to MinDegreesRight gosub(MinDegreesRight) //Calculate rightDegrees radarright(rightdegrees + 180)//Return value of rightdegrees, +180 to point into _pingheading As you can see, this is a very simple task, and now, whenever you need to find the shortest rotation to a certain angle, you can just call MinDegreesRight{} and apply the return value of rightdegrees to the robot part you want rotated. Example2: Another method, which is a little more complex in concept, is passing an array to a function. This invloves the use of the copy(dest,source) command. We need to use the command because passing each property or array element to an RSL function would simply be inefficient, since copy() makes a deepcopy of all elements and properties to the dest variable. Although this example will be less functional than the MindDegreesRight function, the purpose and effectiveness of such a method should be evident. (If necessary, I'll provide a more descript example at a later date) To pass an array to a function, and return the modified array, do the following: The function may look like so. ArrayFunction { '...do stuff to SourceData } To call the ArrayFunction{} and pass array called YourArray copy(SourceData,YourArray) //Pass YourArray to ArrayFunction via SourceData gosub(ArrayFunction) //Do stuff to SourceData copy(YourArray,SourceData) //Return SourceData to YourArray You may also use this method to build a completely new array, or properties. copy(SourceData,YourArray) //Pass YourArray to AnalizeArray via SourceData gosub(AnalizeArray) //Analize SourceData copy(YourArray.Analized,AnalizedData) //Return AnalizedData to YourArray.Analized AnalizeArray { '...Analize SourceData '...Return AnalizedData } For the time being, I'll leave it up to your own creativity to delve into the usefulness of these methods.